package com.woniuxy.yoga.progress.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.ProgressState;
import com.woniuxy.yoga.progress.client.OrderClient;
import com.woniuxy.yoga.progress.dao.model.Expcard;
import com.woniuxy.yoga.progress.dao.mapper.ExpcardMapper;
import com.woniuxy.yoga.progress.model.exception.ProgressException;
import com.woniuxy.yoga.progress.model.exception.ProgressExceptionCode;
import com.woniuxy.yoga.progress.model.param.CreateExpCardParam;
import com.woniuxy.yoga.progress.service.ExpcardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 体验卡流程表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@Service
public class ExpcardServiceImpl extends ServiceImpl<ExpcardMapper, Expcard> implements ExpcardService {

    @Autowired
    private ExpcardMapper expcardMapper;

    @Autowired
    private OrderClient orderClient;

    /**
     * 通过订单编号查询体验卡进度
     * @param orderNum 订单编号
     * @return Expcard
     */
    @Override
    public Expcard getByOrderNum(String orderNum) {
        QueryWrapper<Expcard> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",orderNum);
        Expcard expcard = expcardMapper.selectOne(wrapper);
        if (expcard==null) throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_NO_CARD);
        return expcard;
    }

    /**
     * 通过用户id查询体验卡进度
     * @param userId 订单编号
     * @return Expcard
     */
    @Override
    public Expcard getByUserId(Integer userId) {
        QueryWrapper<Expcard> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Expcard expcard = expcardMapper.selectOne(wrapper);
        if (expcard==null) throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_NO_CARD);
        return expcard;
    }

    /**
     * 创建体验课进度表
     * @param param 创建体验课进度表参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createExpCard(CreateExpCardParam param) {
        //查询是否重复添加
        QueryWrapper<Expcard> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",param.getOrderNum());
        Expcard expcard = expcardMapper.selectOne(wrapper);
        if (expcard!=null) throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_CARD_REPEAT);
        //判断当前用户是否已经购买过体验卡
        QueryWrapper<Expcard> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",param.getUserId());
        Expcard expcard1 = expcardMapper.selectOne(wrapper1);
        if (expcard1!=null) throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_CARD_REPEAT);
        Expcard card = new Expcard();
        card.setOrderNum(param.getOrderNum());
        card.setUserId(param.getUserId());
        card.setExpcardState(ProgressState.EXPCARD_STATE_NO);
        expcardMapper.insert(card);
    }

    /**
     * 完成体验卡
     * @param expcardId 体验卡id
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void finishExpcard(Integer expcardId) {
        //查询进度表
        Expcard expcard = expcardMapper.selectById(expcardId);
        if (Objects.equals(expcard.getExpcardState(), ProgressState.EXPCARD_STATE_DONE))
            throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_CARD_STATE_WRONG);
        //修改状态
        expcard.setExpcardState(ProgressState.EXPCARD_STATE_DONE);
        expcard.setCompleteTime(DateUtil.format(new Date(System.currentTimeMillis()),"yyyy-MM-dd HH:mm:ss"));
        //修改订单状态
        Result<String> result = orderClient.completeOrder(expcard.getOrderNum());
        if (result.getCode()!=200) throw new ProgressException(result.getCode(),result.getMessage());
    }

    /**
     * 查询用户未完成进度表
     * @param userId userid
     * @return Expcard
     */
    @Override
    public Expcard selectUnfinishExp(Integer userId) {
        //创建查询对象
        QueryWrapper<Expcard> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("expcard_state",ProgressState.EXPCARD_STATE_NO);
        Expcard expcard = expcardMapper.selectOne(wrapper);
        if (expcard==null) throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_CARD_NOT_EXIST);
        return expcard;
    }
}
